import { http } from '@/utils/request';


/**
 * 获取购物车总数
 * @param show_type 要显示的类型 all：全部 checked：已选中的
 */
export function getCartCount() {
  return http.request({
    url: `/trade/carts/count`,
    method: "GET",
  });
}


/**
 * 添加货品到购物车
 * @param skuId      产品ID
 * @param num         产品的购买数量
 * @param cartType   	购物车类型，默认加入购物车
 */
export function billOrder(data: any) {
  return http.request({
    url: "/trade/carts",
    method: "POST",
    header: { "content-type": "application/x-www-form-urlencoded" },
    data,
  });
}

/**
 * 支付结果查询
 * @param orderType 交易类型,可用值:TRADE,ORDER,RECHARGE
 * @param sn   订单编号
 */
export function payCallback(params: any) {
  return http.request({
    url: `/payment/cashier/result`,
    method: "GET",
    params,
  });
}

/**
 * 根据交易编号或订单编号查询收银台数据
 * @param params
 */
export function getCashierData(params: any) {
  return http.request({
    url: "payment/cashier/tradeDetail",
    method: "GET",
    params,
  });
}

/**
 * 发起支付
 * @param paymentMethod
 * @param paymentClient
 * @param params
 * @returns {*|*}
 */
export function initiatePay(paymentMethod: string, paymentClient: string, params: any) {
  return http.request({
    url: `payment/cashier/pay/${paymentMethod}/${paymentClient}`,
    method: "GET",
    params,
  });
}


/**
 * 创建交易
 */
export function createTrade(params: any) {
  return http.request({
    url: "/trade/carts/create/trade",
    method: "POST",
    data: params,
  });
}


// 获取购物车列表
export function getCartsALL() {
  return http.request({
    url: `/trade/carts/all`,
    method: "GET",
  });
}
/**
 * 设置全部货品为选中或不选中
 * @param checked
 */
export function checkAll(checked: boolean) {
  return http.request({
    url: "/trade/carts/sku/checked",
    method: "POST",
    params: { checked },
  });
}

/**
 * 设置店铺内全部货品选中状态
 * @param storeId
 * @param checked
 */
export function checkStore(storeId: string, checked: boolean) {
  return http.request({
    url: `/trade/carts/store/${storeId}`,
    method: "POST",
    header: { "content-type": "application/x-www-form-urlencoded" },
    data: { checked },
  });
}


/**
 * 更新购物车货品选中状态
 * @param skuId
 * @param checked
 */
export function checkedSku(skuId: string, checked: boolean) {
  return http.request({
    url: `/trade/carts/sku/checked/${skuId}`,
    method: "POST",
    header: { "content-type": "application/x-www-form-urlencoded" },
    data: { checked },
  });
}


/**
 * 更新购物车商品数量
 * @param skuId
 * @param num
 */
export function changeSkuNum(skuId: string, num = 1) {
  return http.request({
    url: `/trade/carts/sku/num/${skuId}`,
    method: "POST",
    header: { "content-type": "application/x-www-form-urlencoded" },
    data: { num },
  });
}

/**
 * 获取全部配送方式
 */
export function getCanUseDistribution(params: any) {
  return http.request({
    url: `/trade/carts/shippingMethodList`,
    method: "GET",
    params: params,
  });
}


/**
 * 删除多个货品项
 * @param skuIds
 */
export function deleteSkuItem(skuIds: Array<string>) {
  return http.request({
    url: `/trade/carts/sku/remove?skuIds=${skuIds}`,
    method: "DELETE",
  });
}

/**
 * 获取结算参数
 */
export function getCheckoutParams(way: string) {
  return http.request({
    url: "/trade/carts/checked?way=" + way,
    method: "GET",
  });
}



/**
 * 使用优惠券

 */
export function useCoupon(params: any) {
  return http.request({
    url: `/trade/carts/select/coupon`,
    method: "GET",
    params: params,
  });
}
/**
 * 提交配送方式
 * @param params
 */
export function toggleShipMethod(params: any) {
  return http.request({
    url: "/trade/carts/shippingMethod",
    method: "PUT",
    params,
  });
}

/**
 * 设置收货地址ID
 * @param params
 */
export function setStoreAddressId(storeAddressId: string, way: string) {
  return http.request({
    url: `/trade/carts/storeAddress?storeAddressId=${storeAddressId}&way=${way}`,
    method: "GET",

  });
}
